package com.aaa.dang.modules.login.controller;

import com.aaa.dang.common.entity.Employee;
import com.aaa.dang.common.systemlog.annotation.SysLog;
import com.aaa.dang.common.util.ResultModel;
import com.aaa.dang.modules.login.service.ILoginService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import static com.aaa.dang.common.systemconst.ISystemConst.FAULT_CODE;
import static com.aaa.dang.common.systemconst.ISystemConst.SUCCESS_CODE;

/**
 * 登录控制器
 *
 * @author Dadazhao
 * @date 2018/12/21
 */
@Controller
@RequestMapping("/login")
public class LoginController {
    /**
     * 注入登录业务
     */
    @Autowired
    private ILoginService loginService;

    @Autowired
    public ResultModel model;

    /**
     * 前往登录页
     * @return 登录页
     */
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "/login/login";
    }

    /**
     * 前往主页
     * @return 主页
     */
    @RequestMapping("/toIndex")
    public String toIndex(Model m){
        //树形结构的菜单
        ResultModel menu = loginService.getMenuTree();
        //员工信息
        String username = String.valueOf(SecurityUtils.getSubject().getPrincipal());
        Employee employee = loginService.getEmployeeByName(username);
        //员工名称
        //String empName = loginService.getEmployeeName();
        m.addAttribute("employee",employee);
        m.addAttribute("menus",menu.getData());
        return "/login/index";
    }

    /**
     * 登出
     * @return 退出登录
     */
    @RequestMapping("/toLogout")
    @SysLog(operateType= SysLog.Type.LOGOUT,desc = "登出系统")
    public String toLogout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "/login/login";
    }

    /**
     * 判断是否登录成功
     * @return code 0 为成功 1 为失败
     */
    @RequestMapping("/isLogin")
    @ResponseBody
    @SysLog(operateType= SysLog.Type.LOGIN,desc = "登录系统")
    public ResultModel isLogin(Employee employee){
        //获得主体
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(employee.getEmpLoginname(),employee.getEmpPassword());
        try {
            //身份认证
            subject.login(token);
            model.setCode(SUCCESS_CODE);
            model.setMsg("登录成功");
            model.setData("");
            return model;
        }catch (UnknownAccountException e){
            model.setCode(FAULT_CODE);
            model.setMsg("账号不存在");
            model.setData("");
            return model;
        }catch (DisabledAccountException e){
            model.setCode(FAULT_CODE);
            model.setMsg("账号未启用");
            model.setData("");
            return model;
        }catch (IncorrectCredentialsException e){
            model.setCode(FAULT_CODE);
            model.setMsg("密码错误");
            model.setData("");
            return model;
        }

    }

    /**
     * 获得菜单
     * @return 所有菜单
     */
    @RequestMapping("/getMenu")
    @ResponseBody
    public ResultModel getMenu(){
        return loginService.getMenu();
    }

    /**
     * 修改员工的信息
     * @param emp
     * @return
     */
    @RequestMapping("/updateEmployee")
    @ResponseBody
    public ResultModel updateEmployee(Employee emp){
        return loginService.updateEmployee(emp);
    }

    /**
     * 获得当前用户姓名
     * @return 用户名
     */
    @RequestMapping("/getEmployeeName")
    @ResponseBody
    public String getEmployeeName(){
        return loginService.getEmployeeName();
    }
}
